package org.leo.dm.example.plugin;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.leo.dm.component.plugin.recordplugin.RecordPlugin;

public class PluginRecordCustom extends RecordPlugin {

	private int grouplimitcount = 5;
	private int groupcounter = 0;
	private String groupid = UUID.randomUUID().toString();

	private String beforeStr = "";
	private String tempStr;

	@Override
	public Object[] handle(String segmentName, String[] names, Object[] data, int seqmentcounter)  throws Exception{
		List<String> namesList = Arrays.asList(names);
		groupcounter++;

		int positionTemp = namesList.indexOf("LOCAL_ID");
		if (namesList.indexOf("LOCAL_ID") != -1) {
			tempStr = String.valueOf(data[positionTemp]);

			if (groupcounter > grouplimitcount && !beforeStr.equals(tempStr)) {
				groupcounter = 1;
				groupid = UUID.randomUUID().toString();
			}
			beforeStr = tempStr;
		}

		// 新增字段的赋值
		int position = namesList.indexOf("GROUPID");
		if (namesList.indexOf("GROUPID") != -1) {
			data[position] = groupid;
		}

//		log.info("seqmentcounter:" + seqmentcounter);
		return data;
	}

}
